//
// Created by Jisam on 2024/8/7 21:49.
// solve A 采药
#include <bits/stdc++.h>

using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int T , M ;
    cin >> T >> M;
    vector<pair<int,int>> a(M);
    for(int i = 0 ;  i < M ; i ++) cin >> a[i].first >> a[i].second;
    sort(a.begin(),a.end(),[&](pair<int,int> p1,pair<int,int> p2){return p1.second*1.0/p1.first > p2.second*1.0/p2.first;});
    int sumval = 0;
    for(int i = 0 ; i < M ; i ++){
        if(a[i].first <= T){
            sumval += a[i].second;
            T -= a[i].first;
        }
    }
    cout << sumval;
    return 0;
}